package com.fresh.liangyi.dao;

import com.fresh.liangyi.model.domain.Address;
import com.fresh.liangyi.model.domain.Authority;
import com.fresh.liangyi.model.domain.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;


import java.util.List;

@Mapper
@Component
public interface UserMapper {
    //根据id查找用户
    @Select("SELECT * FROM user WHERE id=#{id}")
    public User selectUserWithId(Integer id);
    
    //根据username查找用户
    @Select("select * from user where username=#{username}")
    public User getUserByName(String username);

    //根据用户id找地址
    @Select("SELECT * FROM address WHERE user_id=#{id}")
    public List<Address> getAddressByUserId(Integer id);


    //根据用户id找地址
    @Select("SELECT * FROM address WHERE id=#{id}")
    public Address getAddressById(Integer id);

    //查询用户权限
    @Select("select a.* from " +
            "user c, authority a,user_authority ca " +
            "where ca.user_id=c.id and ca.authority_id=a.id and c.username=#{username}")
    public List<Authority> getAuthority(String username);

    @Insert("INSERT INTO `user`(username,password,email,tel_phone) VALUES(#{username},#{password},#{email},#{telPhone})")
    @Options(useGeneratedKeys = true,keyProperty="id",keyColumn = "id")//返回主键到实体类中
    public Integer addUser(User user);

    //为新建用户分配权限
    @Insert("INSERT INTO `user_authority`(user_id,authority_id) VALUES(#{userId},#{authorityId})")
    public Integer addAuthorityByUser(Integer userId,Integer authorityId);
}
